package top.serms.leetcode;

/**
 * @Author: SerMs
 * @Date: 2024/07/02 09 22
 * @Email: 1839928782@qq.com
 * @Blog: https://serms.top
 **/
public class LeetCode3115 {

    public static void main(String[] args) {
        int[] arr = {4, 2, 9, 5, 3};
        System.out.println(maximumPrimeDifference(arr));
    }

    public static int maximumPrimeDifference(int[] nums) {
        int l = 0, r = 0;
        for (int i = 0; i < nums.length; i++) {
            if (isPrime(nums[i])) {
                l = i;
                break;
            }
        }
        for (int i = nums.length - 1; i >= l; i--) {
            if (isPrime(nums[i])) {
                r = i;
                break;
            }
        }
        return r - l;
    }
   /* public static  int maximumPrimeDifference(int[] nums) {
        List<Integer> indexList = new ArrayList<>();
        for (int i = 0; i < nums.length; i++) {
            if (isPrime(nums[i])) {
                indexList.add(i);
            }
        }
        int size = indexList.size();
        int num = size == 0 ?0: indexList.get(size-1) - indexList.get(0);
        return num;
    }*/

    public static boolean isPrime(int num) {
        if (num <= 1) {
            return false;
        }
        for (int i = 2; i <= Math.sqrt(num); i++) {
            if (num % i == 0) {
                return false;
            }
        }
        return true;
    }
}
